package com.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.reflect.TypeToken;
import com.util.GsonUtil;

/**
 * 优惠券action
 */
@Controller
@RequestMapping("/coupon")
public class CouponController extends BaseAction {

    /**
     * 获取优惠券
     * couponType:券类型（0:优惠券，1:免房券，2:礼遇券）
     * pageIndex:当前几页
     * pageSize：每页几条数据
     * usable:是否可用（0:不可用，1:可用）
     * @return
     */
    @RequestMapping("/getCouponList")
    public ModelAndView getCouponList(HttpServletRequest request,Integer couponType,Integer pageIndex,Integer pageSize,Integer usable){
        ModelAndView md = new ModelAndView("couponList");
        if(couponType==null){
            couponType=0;
        }
        if(usable==null){
            usable=1;
        }
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageSize==null){
            pageSize=20;
        }
        Map<String,Object> promap = new HashMap<String,Object>();
        promap.put("CouponType",couponType);
        promap.put("PageIndex",pageIndex);
        promap.put("PageSize",pageSize);
        promap.put("Usable",usable);
        String memberJson=getHttpJson("Logistics/MEM/GetMemCouponList",GsonUtil.toJson(promap),request,true);
        if("gotoRegister".equals(memberJson)){
            md=new ModelAndView("redirect:/home/gotoRegister");
            return md;
        }

        if(memberJson!=null){
            Map<String,Object> map = GsonUtil.fromJson(memberJson,new TypeToken<Map<String, Object>>(){}.getType());
            if((Double)map.get("stateID")==0&&(Double)map.get("businessState")==0){
                Object tmp = map.get("dataList");
                List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
                if(!"".equals(tmp)){
                    list = (List<Map<String,Object>>)tmp;
                    if(list.size()>0){
                        for(int i =0;i<list.size();i++){
                            double couponTypeStr = (Double)list.get(i).get("CouponType");
                            double couponNumeStr = (Double)list.get(i).get("CouponNum");
                            Integer CouponType = (int)couponTypeStr;
                            Integer CouponNum = (int)couponNumeStr;
                            list.get(i).put("CouponType",CouponType);
                            list.get(i).put("CouponNum",CouponNum);
                            list.get(i).put("StartDate",DateUtil.formatDate(DateUtil.parseDate(list.get(i).get("StartDate").toString()),"yyyy-MM-dd"));
                            list.get(i).put("EndDate",DateUtil.formatDate(DateUtil.parseDate(list.get(i).get("EndDate").toString()),"yyyy-MM-dd"));
                        }
                    }
                }
                md.addObject("dataList",list);
                md.addObject("couponType",couponType);
            }


        }
        return md;
    }

    /**
     * 获取免房券
     * @return
     */

    @RequestMapping("/getFreeRoomList")
    public ModelAndView getFreeRoomList(HttpServletRequest request){
        ModelAndView md = new ModelAndView("couponList");
        Map<String,Object> promap = new HashMap<String,Object>();
        String memberJson=getHttpJson("Logistics/HOS/getFreeRoomList",GsonUtil.toJson(promap),request,true);
        if("gotoRegister".equals(memberJson)){
            md=new ModelAndView("redirect:/home/gotoRegister");
            return md;
        }
        if(memberJson!=null){
            Map<String,Object> map = GsonUtil.fromJson(memberJson,new TypeToken<Map<String, Object>>(){}.getType());
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            if((Double)map.get("stateID")==0&&(Double)map.get("businessState")==0){
                Object tmp = map.get("dataList");

                if(!"".equals(tmp)){
                    list = (List<Map<String,Object>>)tmp;
                    if(list.size()>0){
                        for(int i =0;i<list.size();i++){
                            double takeTypeStr = (Double)list.get(i).get("TakeType");
                            double invoiceTypeStr = (Double)list.get(i).get("InvoiceType");
                            Integer TakeType = (int)takeTypeStr;
                            Integer InvoiceType = (int)invoiceTypeStr;
                            list.get(i).put("TakeType",TakeType);
                            list.get(i).put("InvoiceType",InvoiceType);
                        }
                    }
                }
            }

            md.addObject("dataList",list);
        }
        return md;
    }

}
